<?php

namespace addons\booking\controller;

use addons\booking\model\StoreUser as StoreUserModel;
use think\Db;
use think\Session;


class StoreUser extends Base
{
    protected $noNeedLogin = ['storeList', 'detail', 'defaultStore', 'cutStore'];
    protected $modelValidate = true; //是否开启Validate验证，默认是false关闭状态
    protected $modelSceneValidate = true; //是否开启模型场景验证，默认是false关闭状态

    //发单员列表
    public function storeUserList()
    {
        $this->success('', StoreUserModel::tableList($this->request->param()));
    }

    //添加/编辑
    public function save()
    {
        $id = $this->request->post('id');
        $store_id = $this->request->post('store_id');
        $user_name = $this->request->post('user_name');
        $mobile = $this->request->post('mobile');
        $image = $this->request->post('image');
        $identity = $this->request->post('identity',1);

        if(!$store_id)  $this->error('参数错误');
        if(!$user_name)  $this->error('请填写姓名');
        if(!$mobile)  $this->error('请填手机号码');
        if(!$image)  $this->error('请上传头像');
        if(!$identity)  $this->error('请选择身份');

        $user = Db::name('user')->where('mobile',$mobile)->find();
        if(!$user) $this->error('用户不存在');

        //发单员信息
        $info = StoreUserModel::where(['store_id'=>$store_id,'uid'=>$user['id']])->find();

        $map['store_id'] = $store_id;
        $map['user_name'] = $user_name;
        $map['mobile'] = $mobile;
        $map['image'] = $image;
        $map['identity'] = $identity;
        $map['uid'] = $user['id'];
        $res = true;
        if(!$id){
            $map['createtime'] = time();
            $res = StoreUserModel::insert($map);
        } else {
            $map['updatetime'] = time();
            $res = StoreUserModel::where('id',$info['id'])->update($map);
        }

        if($res) {
            $this->success('提交成功');
        } else {
            $this->error('提交失败');
        }

    }

    //启用/禁用发单员
    public function set_status()
    {
        $id = $this->request->get('id');
        if(!$id)  $this->error('参数错误');

        $info = StoreUserModel::where('id',$id)->find();
        if(!$info)  $this->error('数据不存在');

        $status = 0;
        if($info['status'] == 0){
            $status = 1;
        }
        $map['status'] = $status;
        $map['updatetime'] = time();
        $res = StoreUserModel::where('id',$info['id'])->update($map);
        if($res) {
            $info = StoreUserModel::where('id',$id)->find();
            $this->success('提交成功',['status'=>$info['status']]);
        } else {
            $this->error('提交失败');
        }

    }

    //删除
    public function del()
    {
        $id = $this->request->get('id');
        if(!$id)  $this->error('参数错误');
        $info = StoreUserModel::where('id',$id)->find();
        if(!$info)  $this->error('数据不存在');

        $map['status'] = 0;
        $map['deletetime'] = time();
        $res = StoreUserModel::where('id',$id)->update($map);

        if($res) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    //设为值班/下班
    public function set_identity()
    {
        $id = $this->request->get('id');
        $store_id = $this->request->get('store_id');
        if(!$id || !$store_id)  $this->error('参数错误');
        $info = StoreUserModel::where('id',$id)->where('store_id',$store_id)->find();
        if(!$info)  $this->error('数据不存在');

        $work_status = 0;
        if($info['work_status'] == 0){
            $work_status = 1;
        }

        $map['work_status'] = $work_status;
        $map['updatetime'] = time();
        if($work_status == 1){
            StoreUserModel::where('store_id',$info['store_id'])->update(['work_status' => 0]);
        }

        $res = StoreUserModel::where('id',$info['id'])->update($map);
        if($res) {
            $info = StoreUserModel::where('id',$info['id'])->find();
            $this->success('提交成功',['work_status'=>$info['work_status']]);
        } else {
            $this->error('提交失败');
        }
    }

}
